package com.db.compare.interceptor;

import com.db.compare.core.BaseController;
import com.db.compare.core.BaseResponse;
import com.db.compare.exception.BuinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class AdminExceptionHandler extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdminExceptionHandler.class);

    /**
     * @Description: 系统异常捕获处理
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public BaseResponse javaExceptionHandler(Exception ex) {
        logger.error("捕获到Exception异常",ex);
        //异常日志入库

        return buildFailResponse(ex.getMessage());
    }

    /**
     * @Description: 自定义异常捕获处理
     */
    @ResponseBody
    @ExceptionHandler(value = BuinessException.class)//MessageCenterException是自定义的一个异常
    public BaseResponse messageCenterExceptionHandler(BuinessException ex) {
        logger.error("捕获到MessageCenterException异常",ex.getMessage());
        //异常日志入库

        return buildFailResponse();
    }

}
